import { professionalTrainingCreate, professionalTrainingUpdate } from "@/api/SecondmentDictionary";
import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { message } from "antd";
import { useState } from "react";
import { useTranslation } from "react-i18next";
let getProfessionalTraining: Function;
let profeId: string;
const timeFn = (tiem: string) => {
	const [date] = tiem.split(" ");
	return date;
};
const index = (props: { register: Fn }) => {
	const { t } = useTranslation();
	const formSchema: FormSchema[] = [
		{
			label: t("felix_ui.staff_id"),
			field: "staffId", //员工id
			component: "Input"
		},
		{
			label: t("felix_ui.full_name"),
			field: "fullName", //员工姓名
			component: "Input"
		},
		{
			label: t("felix_ui.birthday"),
			field: "birthday", //生日
			component: "DatePicker"
		},
		{
			label: t("felix_ui.gender"),
			field: "gender", //性别
			component: "Select",
			componentProps: {
				options: [
					{ label: t("kevin_ui.male"), value: 0 },
					{ label: t("kevin_ui.female"), value: 1 }
				]
			}
		},
		{
			label: t("felix_ui.trainingContent"),
			field: "trainingContent", //培训内容
			component: "Input"
		},
		{
			label: t("felix_ui.trainingLocation"),
			field: "trainingWorkplace", //培训地点
			component: "Input"
		},
		{
			label: t("felix_ui.instructor"),
			field: "instructor", //教练
			component: "Input"
		},
		{
			label: t("felix_ui.poz_level"),
			field: "pozLevel", //水平
			component: "Select",
			componentProps: {
				options: [
					{ label: t("secondmentDictionary.bachelorOfNursing"), value: "1" },
					{ label: t("secondmentDictionary.nurse"), value: "2" },
					{ label: t("secondmentDictionary.technician"), value: "3" },
					{ label: t("secondmentDictionary.associateProfessor"), value: "4" },
					{ label: t("kevin_ui.doctor"), value: "5" }
				]
			}
		},
		{
			label: t("felix_ui.unit"),
			field: "workplace", //工作地址
			component: "Input"
		}
	];

	const [isAdd, setIsAdd] = useState(true);
	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		getProfessionalTraining = data.getProfessionalTraining;
		resetFields();
		if (data.record) {
			profeId = data.record.profeId;
			setFieldsValue(data.record);
			setIsAdd(false);
		}
	});

	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 180,
		baseColProps: {
			span: 24
		}
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		let api = professionalTrainingCreate;
		if (!isAdd) {
			api = professionalTrainingUpdate;
		}
		formValue.birthday = timeFn(formValue.birthday);
		const res = await api({ ...formValue, profeId });
		if (res) {
			profeId ? message.success(t("kevin_ui.addedSuccessfully")) : message.success(t("kevin_ui.updateSuccess"));
			getProfessionalTraining();
			closeDrawer();
		}
	};
	return (
		<BasicDrawer showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={register}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
